﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Presenters.Users;
//using University.Workshop.Presenters.Users.Search;

namespace University.Workshop.Presentation.Users
{
    /// <summary>
    /// Interaction logic for SearchUser.xaml
    /// </summary>
    public partial class SearchUser : Page, ISearchUserView
    {
        PresenterSearchUser pSearchUser;

        public SearchUser()
        {
            InitializeComponent();
            pSearchUser = new PresenterSearchUser(this);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            App.Current.MainWindow.Title = "Buscar Usuario";
        }

        private void SearchBtn_Click(object sender, RoutedEventArgs e)
        {
            pSearchUser.SearchUser();
        }

        public ICollection<Model.User> UserFinded
        {
            get
            {
                if (UserFindedDgv.ItemsSource == null)
                    new List<Model.User>();
                return (ICollection<Model.User>)UserFindedDgv.ItemsSource;
            }
            set
            {
                UserFindedDgv.ItemsSource = null;
                UserFindedDgv.ItemsSource = value;
            }
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Error!!!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            MessageBox.Show(App.Current.MainWindow, MessageValue, "Atencion!!!", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.RightAlign);
        }

        private void ShowUserBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Users.PageReadView((Model.User)UserFindedDgv.SelectedItem));
        }

        private void ModifyUserBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Users.PageModifyView((Model.User)UserFindedDgv.SelectedItem));
        }



        public string StateUser
        {
            get
            {
                return UserStateDdl.SelectionBoxItem.ToString();
            }
        }

        public string Login
        {
            get
            {
                return LoginTxBx.Text;
            }
            set
            {
                LoginTxBx.Text = value;
            }
        }

        public string PersonLastNamesRelative
        {
            get
            {
                return lastNamesTxBx.Text;
            }
            set
            {
                lastNamesTxBx.Text = value;
            }
        }

        public string PersonNamesRelative
        {
            get
            {
                return NamesTxBx.Text;
            }
            set
            {
                NamesTxBx.Text = value;
            }
        }


        private void DisableUser_Click(object sender, RoutedEventArgs e)
        {
                pSearchUser.ShowDisableUser();

        }

        private void EnableUser_Click(object sender, RoutedEventArgs e)
        {
                pSearchUser.ShowDisableUser();
        }






        private void StateBtn_Click(object sender, RoutedEventArgs e)
        {
            //this.NavigationService.Navigate(new Cistern.Modify.ModifyCisternPrincipal((Model.Cistern)UserFindedDgv.SelectedItem));
        }






        public bool NotifyConfirmationMessage(string MessageValue)
        {
            return MessageBox.Show(App.Current.MainWindow,
                MessageValue,
                "Atención!!!",
                MessageBoxButton.YesNo,
                MessageBoxImage.Information,
                MessageBoxResult.Yes,
                MessageBoxOptions.RightAlign) == MessageBoxResult.Yes;
        }

        public void OpenDisableUserPopUp(Model.User u)
        {
            if (u != null)
            {
                Back1.Visibility = Visibility.Visible;
                BackDisable.Visibility = Visibility.Visible;
                PersonCiTxt.Content = u.Person.PersonId;
                PersonNamesForDisableTxt.Content = u.Person.Names;
                PersonLastNamesForDisableTxt.Content = u.Person.LastNames;
                
                PersonLoginForDisabelLbl.Content = u.Login;
                //PersonRoleForDisableLbl.Content = u.Role;

                switch (u.Role)
                {
                    case 0: PersonRoleForDisableLbl.Content = "Administrador";
                        break;
                    case 1: PersonRoleForDisableLbl.Content = "Operador";
                        break;
                    case 2: PersonRoleForDisableLbl.Content = "Presidente";
                        break;
                    case 3: PersonRoleForDisableLbl.Content = "Tesorero";
                        break;
                    default: PersonRoleForDisableLbl.Content = "-";
                        break;
                }

                IsActiveChkB.IsChecked = u.IsActive;
                if (u.IsActive)
                    IsActiveChkB.Content = "Activo";
                else
                    IsActiveChkB.Content = "Inactivo";


                App.Current.MainWindow.Title = "Estado Usuario";

                if (!u.IsActive)
                    button2.Content = "Habilitar";
                else
                    button2.Content = "Inhabilitar";
            }
        }


        public Model.User UserSelected
        {
            get { return (Model.User)UserFindedDgv.SelectedItem; }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (pSearchUser.UserForDisable.IsActive)
                pSearchUser.DisableUser();
            else
                pSearchUser.EnableUser();

        }


        public void HideDisableUser()
        {
            Back1.Visibility = Visibility.Hidden;
            BackDisable.Visibility = Visibility.Hidden;
            PersonCiTxt.Content = string.Empty;
            PersonNamesForDisableTxt.Content = string.Empty;
            PersonLastNamesForDisableTxt.Content = string.Empty;
            PersonLoginForDisabelLbl.Content = string.Empty;
            PersonRoleForDisableLbl.Content = string.Empty;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            pSearchUser.HideDisableUser();
        }


        public void GoToModifyUser(Model.User user)
        {
            this.NavigationService.Navigate(new PageModifyView(user));
        }

        private void CantCupyPasteOrCut(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Copy)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Cut)
                e.Handled = true;
            if (e.Command == ApplicationCommands.Paste)
                e.Handled = true;
        }


        private void PersonNamesTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsLetter(ch));
            }
        }

        private void UsernamesTxt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void UsernameTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string temporal = ((TextBox)sender).Text + e.Text;
            foreach (char ch in e.Text)
            {
                e.Handled = !(char.IsLetterOrDigit(ch) || ch.Equals('_'));
            }
        }

        private void LoginTxBx_PreviewKeyDown(object sender, KeyEventArgs e)
        {

        }


    }
}
